package com.imooc.mall.controller;

import com.github.pagehelper.PageInfo;
import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.common.Constant;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.pojo.Category;
import com.imooc.mall.model.pojo.User;
import com.imooc.mall.model.request.AddCategoryReq;
import com.imooc.mall.model.request.UpdateCategoryReq;
import com.imooc.mall.service.CategoryService;
import com.imooc.mall.service.UserService;
import com.imooc.mall.vo.CategoryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RecursiveAction;

/**
 * 目录的controller
 */

@Controller
public class CategoryController {

    @Autowired
    private UserService userService;

    @Autowired
    private CategoryService categoryService;



    @ApiOperation("后台添加商品分类")
    @PostMapping("admin/category/add")
    @ResponseBody
    public ApiRestResponse addCategory(HttpSession session,
                                        @RequestBody AddCategoryReq categoryReq){
        //判断前端参数是否为空
        if (categoryReq.getName() == null ||
                categoryReq.getOrderNum() ==null ||
                categoryReq.getParentId() ==null ||
                categoryReq.getType()==null){
            return ApiRestResponse.error(ImoocMallExceptionEnum.PARA_NOT_NULL);
        }
        //判断当前用户是否已经登录
        User currentUser = (User) session.getAttribute(Constant.IMOOC_MALL_USER);
        if (currentUser==null){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGION);
        }
        //判断该用户是否是管理员
        Boolean aBoolean = userService.checkAdminRole(currentUser);
        if (aBoolean){
            categoryService.add(categoryReq);
            return ApiRestResponse.success();

        }else {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
        }
    }

    @ApiOperation("后台更新商品分类")
    @PostMapping("admin/category/update")
    @ResponseBody
    public ApiRestResponse update(@RequestBody UpdateCategoryReq updateCategoryReq,
                                  HttpSession session){
        //判断当前用户是否已经登录
        User currentUser = (User) session.getAttribute(Constant.IMOOC_MALL_USER);
        if (currentUser==null){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGION);
        }
        //判断该用户是否是管理员
        Boolean aBoolean = userService.checkAdminRole(currentUser);
        if (aBoolean){
            Category category = new Category();
            BeanUtils.copyProperties(updateCategoryReq,category);
            categoryService.update(category);
            return ApiRestResponse.success();

        }else {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
        }

    }

    /**
     * 删除商品功能
     */
    @ApiOperation("后台删除商品分类")
    @PostMapping("admin/category/delete")
    @ResponseBody
    public ApiRestResponse delete(@RequestParam Integer id){
        categoryService.delete(id);
        return ApiRestResponse.success();
    }

    /**
     * 后台分页查询页面
     */
    @ApiOperation("后台分页目录商品分类")
    @GetMapping("admin/category/list")
    @ResponseBody
    public ApiRestResponse listCategoryForAdmin(@RequestParam Integer pageNum,
                                                @RequestParam Integer pageSize){

        PageInfo pageInfo = categoryService.listForAdmin(pageNum, pageSize);
        return ApiRestResponse.success(pageInfo);
    }


    @ApiOperation("前台分页目录商品分类")
    @GetMapping("category/list")
    @ResponseBody
    public ApiRestResponse listCategoryForCustomer(){
        List<CategoryVo> categoryVos = categoryService.listCategoryForCustomer(0);
        return ApiRestResponse.success(categoryVos);

    }



}
